<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加产品</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/frame.css">

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>

    <script type="text/javascript" charset="utf-8" src="/static/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/static/ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="/static/ueditor/lang/zh-cn/zh-cn.js"></script>

    <style>
        .out-box{
            background-color: #f3f3f3;
            padding: 15px 0;
            margin-bottom: 20px;
        }
        .layui-upload-img { width: 90px; height: 90px; margin: 0; }
        .pic-more { width:100%; margin: 10px 0px 0px 0px;}
        .pic-more li { width:90px; float: left; margin-right: 5px;}
        .pic-more li .layui-input { display: initial; }
        .pic-more li a { position: absolute; top: 0; display: block; }
        .pic-more li a i { font-size: 24px; background-color: #008800; }
        #slide-pc-priview .item_img img{ width: 90px; height: 90px;}
        #slide-pc-priview li{position: relative;}
        #slide-pc-priview li .operate{ color: #000; display: none;}
        #slide-pc-priview li .toleft{ position: absolute;top: 40px; left: 1px; cursor:pointer;}
        #slide-pc-priview li .toright{ position: absolute;top: 40px; right: 1px;cursor:pointer;}
        #slide-pc-priview li .close{position: absolute;top: 5px; right: 5px;cursor:pointer;}
        #slide-pc-priview li:hover .operate{ display: block;}
    
        #slide-pc-priview1 .item_img img{ width: 90px; height: 90px;}
        #slide-pc-priview1 li{position: relative;}
        #slide-pc-priview1 li .operate{ color: #000; display: none;}
        #slide-pc-priview1 li .toleft{ position: absolute;top: 40px; left: 1px; cursor:pointer;}
        #slide-pc-priview1 li .toright{ position: absolute;top: 40px; right: 1px;cursor:pointer;}
        #slide-pc-priview1 li .close{position: absolute;top: 5px; right: 5px;cursor:pointer;}
        #slide-pc-priview1 li:hover .operate{ display: block;}
    </style>
</head>
<body style="background-color: rgb(255, 255, 255);">           
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>添加产品</legend>
</fieldset>

<form class="layui-form layui-form-pane" action="" lay-filter="example" style="padding: 20px">
    <div class="layui-form-item" style="display: flex;">
        <label class="layui-form-label">展示图片</label>
        <div class="layui-input-inline">
            <div class="fup_files">
                <input type="file" name="show_img" onchange="fuploads($(this))">
                <img src="" class="img" name="img" width="100%" height="100%" style="display: none" />
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入名称" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">分类</label>
        <div class="layui-input-inline" style="width: 40%">
            <select name="cate_id">
                <option value="">-- 请选择分类 --</option>
                {foreach $cate_list as $k => $v}
                <option value="{$v.id}">{$v.name}</option>
                {/foreach}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="price" lay-verify="price" autocomplete="off" placeholder="请输入价格" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item" pane="" style="width: 47.5%">
        <label class="layui-form-label">规格</label>
        <div class="layui-input-block">
            {foreach $sku as $k => $v}
            <input type="checkbox" name="sku_id" value="{$v.id}" title="{$v.name}">
            {/foreach}
        </div>
    </div>

    <div class="layui-form-item" id="pics">
        <div class="layui-form-label">产品轮播图</div>
        <div class="layui-input-block" style="width: 70%;">
            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-primary pull-left" id="slide-pc">选择多图</button>
                <div class="pic-more">
                    <ul class="pic-more-upload-list" id="slide-pc-priview"></ul>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">产品介绍</label>
        <div class="layui-input-inline" style="width: 40%">
            <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
            <a href="javascript:history.back(-1)" class="layui-btn layui-btn-primary">返回</a>
        </div>
    </div>
</form>
          
<script src="/static/layui/layui.js" charset="utf-8"></script>

<script>
    var ue = UE.getEditor('editor');

    //图片上传
    function fuploads(f){
        var nameAll = f[0].files[0].type;
        var name = nameAll.split("/");
        
        if(name[0] != 'image'){
            layer.msg('上传类型错误', {icon: 2});
        }else{
            var url = window.URL.createObjectURL(f[0].files[0]);  // 得到bolb对象路径，可当成普通的文件路径一样使用
            f.next("img").css('display', 'block');
            f.next("img").attr('src', url);
        }
    }

    var goodsImgBanner = [];
    layui.use('upload', function(){
        var $ = layui.jquery;
        var upload = layui.upload;

        upload.render({
            elem: '#slide-pc',
            url: '{:url("Product/img_up")}',
            size: 50000,
            exts: 'jpg|png|jpeg',
            multiple: true,
            before: function(obj) {
                layer.msg('图片上传中...', {
                    icon: 16,
                    shade: 0.01,
                    time: 0
                })
            },
            done: function(res) {
                goodsImgBanner.push(res.data);

                layer.close(layer.msg());  // 关闭上传提示窗口

                if(res.status == 0) {
                    return layer.msg(res.msg);
                }

                $('#slide-pc-priview').append('<li class="item_img"><div class="operate"><i class="toleft layui-icon"></i><i class="toright layui-icon"></i><i  class="close layui-icon"></i></div><img src="' + res.data + '" class="img bannerimg" ><input type="hidden" name="pc_src[]" class="input2" value="' + res.data + '" /></li>');
            }
        });
    });
        
    //点击多图上传的X,删除当前的图片
    $("body").on("click",".close",function(){
        var that = $(this).closest("li").remove();
    });

    // 多图上传点击<>左右移动图片
    $("body").on("click", ".pic-more ul li .toleft", function(){
        var li_index = $(this).closest("li").index();

        if(li_index >= 1){
            $(this).closest("li").insertBefore($(this).closest("ul").find("li").eq(Number(li_index)-1));
        }
    });

    $("body").on("click", ".pic-more ul li .toright", function(){
        var li_index = $(this).closest("li").index();
        $(this).closest("li").insertAfter($(this).closest("ul").find("li").eq(Number(li_index) + 1));
    });

    layui.use(['form', 'layedit'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit;

        // 监听提交
        form.on('submit(submit)', function(data){
            let bannerImg = JSON.stringify([...$(".bannerimg")].map((val,index) => {
                return {
                    url: $(val).attr("src"),
                    sort: index+1
                }
            }))

            let sku_id = ''
            $("input[name='sku_id']:checked").each(function(){
                sku_id += $(this).val() + ','
            })

            let formData = new FormData();
            let img = $("input[name='show_img");

            formData.append('name', data.field.name);
            formData.append('price', data.field.price);
            formData.append('show_img', img[0].files[0]);
            formData.append('banner_img', bannerImg);
            formData.append('sku_id', sku_id);
            formData.append('content', ue.getContent());

            if(!data.field.cate_id){
                data.field.cate_id = 0
            }
            
            formData.append('cate_id', data.field.cate_id);

            // 加载中样式
            let loading = layer.msg('正在添加，请稍后...', {icon: 16, shade: 0.3, time:0});

            $.ajax({
                url:'{:url("Product/product_add_data")}',
                data:formData,
                contentType: false,  // 必须false才会自动加上正确的Content-Type
                processData: false,
                type:"post",
                dataType:'json',
                success:function(res){
                    layer.close(loading);

                    if(res.code == 0){
                        layer.msg(res.msg, {icon: 1,time: 1500}, function(){
                            history.back(-1);
                        });
                    }else{
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error:function(res){
                    layer.msg(res.msg, {icon: 2});
                }
            })

            return false;
        });
    });
</script>
</body>
</html>